package com.example.iceserverdemo.meeting.controllers;

import com.example.iceserverdemo.meeting.models.MeetingRoom;
import com.example.iceserverdemo.meeting.services.MeetingService;
import com.example.iceserverdemo.utils.TimeUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/meeting")
public class MeetingController {
    @GetMapping("getNewRoom")
    Object getNewRoom() {
        Map<String, Object> result = new HashMap();
        String roomNumber = MeetingService.getNewRoomNumber();
        while (MeetingService.containsRoom(roomNumber))
            roomNumber = MeetingService.getNewRoomNumber();
        MeetingRoom room = new MeetingRoom();
        room.number = roomNumber;
        result.put("data", room);
        result.put("result", true);
        return result;
    }

    @PostMapping("addRoom")
    Object addRoom(@RequestBody MeetingRoom room) {
        if (room.members == null)
            room.members = new ArrayList();
        room.createTime = TimeUtil.getNowString();

        Map<String, Object> result = new HashMap();
        if (!MeetingService.containsSession(room.master)) {
            result.put("result", false);
            result.put("message", "Master offline!");
            return result;
        }

        if (MeetingService.containsRoom(room.number)) {
            result.put("result", false);
            result.put("message", "Room has existed!");
            return result;
        }

        MeetingService.addRoom(room);
        result.put("data", room);
        result.put("result", true);
        return result;
    }

    @GetMapping("getAllRooms")
    Object getAllRooms() {
        Map<String, Object> result = new HashMap();
        result.put("data", MeetingService.getAllRooms());
        result.put("result", true);
        return result;
    }

    //@PostMapping("deleteRoom")
    Object deleteRoom(String roomNumber) {
        Map<String, Object> result = new HashMap();
        if (!MeetingService.containsRoom(roomNumber)) {
            result.put("result", false);
            result.put("message", "room not exist!");
            return result;
        }
        MeetingService.removeRoom(roomNumber);
        result.put("result", true);
        return result;
    }

    @GetMapping("deleteAllRooms")
    Object deleteAllRooms() {
        MeetingService.clearRooms();
        Map<String, Object> result = new HashMap();
        result.put("result", true);
        return result;
    }

    @PostMapping("addRoomMember")
    Object addRoomMember(String roomNumber, String member) {
        Map<String, Object> result = new HashMap();
        if (MeetingService.containsRoom(roomNumber)) {
            MeetingRoom room = MeetingService.getRoom(roomNumber);
            if (!room.members.contains(member))
                room.members.add(member);
            result.put("data", room);
            result.put("result", true);
            return result;
        }
        result.put("result", false);
        result.put("message", "room not exist!");
        return result;
    }

    @PostMapping("deleteRoomMember")
    Object deleteRoomMember(String roomNumber, String member) {
        Map<String, Object> result = new HashMap();
        if (MeetingService.containsRoom(roomNumber)) {
            MeetingRoom room = MeetingService.getRoom(roomNumber);
            if (room.members.contains(member))
                room.members.remove(member);
            result.put("data", room);
            result.put("result", true);
            return result;
        }
        result.put("result", false);
        result.put("message", "room not exist!");
        return result;
    }

    @PostMapping("changeRoomMaster")
    Object changeRoomMaster(String roomNumber, String master) {
        Map<String, Object> result = new HashMap();
        if (MeetingService.containsRoom(roomNumber)) {
            MeetingRoom room = MeetingService.getRoom(roomNumber);
            room.master = master;
            result.put("data", room);
            result.put("result", true);
            return result;
        }
        result.put("result", false);
        result.put("message", "room not exist!");
        return result;
    }

    @GetMapping("getSessions")
    Object getSessions() {
        Map<String, Object> result = new HashMap<>();
        result.put("data", MeetingService.getSessions());
        return result;
    }
}
